package org.system.core.utils;

import com.github.pagehelper.PageHelper;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.system.core.common.Constants;

/**
 * @author baoRoot
 */
public class PageUtils extends PageHelper {

    public static ServletRequestAttributes getRequestAttributes() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        return (ServletRequestAttributes) attributes;
    }

    public static String getParameter(String name) {
        return getRequestAttributes().getRequest().getParameter(name);
    }

    public static void startPage(){
        String pageNumStr = getParameter(Constants.PAGE_NUM);
        String pageSizeStr = getParameter(Constants.PAGE_SIZE);
        Integer pageNum = ConvertUtils.toInt(pageNumStr, Constants.DEFAULT_PAGE_NUM);
        Integer pageSize = ConvertUtils.toInt(pageSizeStr, Constants.DEFAULT_PAGE_NUM);
        if (pageSize > 500) {
            pageSize = 500;
        }
        if (pageSize < 5) {
            pageSize = Constants.DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(pageNum, pageSize);
    }

}
